<?php
/**
*
* @package phpBB3
* @copyright (c) 2009 kmklr72
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

class chat
{
	// Get the MOD config for the ACP
	function get_chat_config()
	{
		global $db;

		$chat_config = array();
	
		$sql = "SELECT * FROM " . CHAT_CONFIG_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$chat_config[$row['config_name']] = $row['config_value'];
		}
		$db->sql_freeresult($result);

		return $chat_config;
	}

	// Update settings in the config
	function set_chat_config($config_name, $config_value)
	{
		global $db, $chat_config;

		$sql = "UPDATE " . CHAT_CONFIG_TABLE . "
			SET config_value = '" . $db->sql_escape($config_value) . "'
			WHERE config_name = '" . $db->sql_escape($config_name) . "'";
		$db->sql_query($sql);
	}

	// Check for banned flags, enabled flags, etc
	function check_errors()
	{
		global $db, $user, $auth, $template;

		$chat_config = $this->get_chat_config();

		$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user->data['user_id'];
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$user_banned = $row['user_chat_banned'];
		}
		$db->sql_freeresult($result);

		if ($chat_config['chat_enabled'] == 0)
		{
			$template->assign_vars(array(
				'S_CHAT_ERROR'				=> true,
				'CHAT_ERROR_MESSAGE'		=> $chat_config['chat_disabled_message'],
			));
		}
		if ($chat_config['permissions_enabled'] == 1 && !$auth->acl_get('u_chat_mod') && $user->data['user_id'] != ANONYMOUS)
		{
			$template->assign_vars(array(
				'S_CHAT_ERROR'				=> true,
				'CHAT_ERROR_MESSAGE'		=> $user->lang['CHAT_NO_PERMISSIONS'],
			));
		}
		if ($chat_config['guest_view_enabled'] == 0 && $user->data['user_id'] == ANONYMOUS)
		{
			$template->assign_vars(array(
				'S_CHAT_ERROR'				=> true,
				'CHAT_ERROR_MESSAGE'		=> $user->lang['CHAT_GUEST_VIEW_DISABLED'],
			));
		}
		if ($user_banned == 1)
		{
			$template->assign_vars(array(
				'S_CHAT_ERROR'				=> true,
				'CHAT_ERROR_MESSAGE'		=> $user->lang['CHAT_USER_BANNED_MESSAGE'],
			));
		}

		return;
	}

	// Get online users list
	function whois_online()
	{
		global $db, $template, $user;
		global $delay, $last_post, $session_time;

		$check_time = time() - $session_time;

		$sql_ary = array(
			'username'			=> $user->data['username'],
			'user_colour'		=> $user->data['user_colour'],
			'user_lastupdate'	=> time(),
		);
		$sql = 'UPDATE ' . CHAT_SESSIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE user_id = {$user->data['user_id']}";
		$db->sql_query($sql);

		$sql = 'DELETE FROM ' . CHAT_SESSIONS_TABLE . " WHERE user_lastupdate < $check_time";
		$db->sql_query($sql);

		$sql = 'SELECT *
			FROM ' . CHAT_SESSIONS_TABLE . "
			WHERE user_lastupdate > $check_time
			ORDER BY username ASC";
		$result = $db->sql_query($sql);

		$status_time = time();
		while ($row = $db->sql_fetchrow($result))
		{
			if ($row['user_id'] == $user->data['user_id'])
			{
				$last_post = $row['user_lastpost'];
				$login_time = $row['user_login'];
				$status_time = ($last_post > $login_time) ? $last_post : $login_time;
			}
			$status = $this->get_status($row['user_lastpost']);
			$template->assign_block_vars('whoisrow', array(
				'USERNAME_FULL'	=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $user->lang['GUEST']),
				'USER_STATUS'	=> $status,
			));
		}
		$db->sql_freeresult($result);

		$template->assign_vars(array(
			'DELAY'			=> ($status_time) ? $delay[$this->get_status($status_time)] : $delay['idle'],
			'LAST_TIME'		=> time(),
			'S_WHOISONLINE'	=> true,
		));
		return false;
	}

	// Get user status
	function get_status($last)
	{
		global $times;

		$status = 'online';
		if ($last < (time() - $times['offline']))
		{
			$status = 'offline';
		}
		else if ($last < (time() - $times['idle']))
		{
			$status = 'idle';
		}
		return $status;
	}

	// Get list of channels
	function get_channel_list($in_acp = false)
	{
		global $db, $template, $phpbb_root_path, $phpEx;

		$sql = 'SELECT * FROM ' . CHAT_CHANNELS_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$template->assign_block_vars('channel_list', array(
				'CHANNEL_ID'			=> $row['channel_id'],
				'CHANNEL_NAME'			=> $row['channel_name'],
				'CHANNEL_PASSWORD'		=> $row['channel_password'],
				'LAST_MESSAGE_ID'		=> $row['last_message_id'],
				'LAST_MESSAGE_TEXT'		=> $row['last_message_text'],
				'LAST_POSTER_ID'		=> $row['last_poster_id'],
				'LAST_POSTER_USERNAME'	=> $row['last_poster_username'],
				'LAST_POSTER_COLOR'		=> $row['last_poster_color'],

				// Use a ternary statement to check to see if we are in the ACP. Might change this behavior later.
				'U_CHAT_CHANNEL_EDIT'	=> ($in_acp == true) ? append_sid("{$phpbb_root_path}adm/index.$phpEx", 'i=chat&amp;mode=chan_edit&amp;cid=' . $row['channel_id'], true) : '',
				'U_CHAT_CHANNEL_DELETE'	=> ($in_acp == true) ? append_sid("{$phpbb_root_path}adm/index.$phpEx", 'i=chat&amp;mode=chan_list&amp;del=1&amp;cid=' . $row['channel_id'], true) : '',
			));
		}
		$db->sql_freeresult($result);

		return;
	}

	// Get channel information
	function get_channel_info($channel_id)
	{
		global $db;

		$sql = 'SELECT * FROM ' . CHAT_CHANNELS_TABLE . ' WHERE channel_id = ' . $channel_id;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$channel = array(
				'channel_id'			=> $channel_id,
				'channel_name'			=> $row['channel_name'],
				'channel_password'		=> $row['channel_password'],
				'last_message_id'		=> $row['last_message_id'],
				'last_message_text'		=> $row['last_message_text'],
				'last_poster_id'		=> $row['last_poster_id'],
				'last_poster_username'	=> $row['last_poster_username'],
				'last_poster_color'		=> $row['last_poster_color'],
			);
		}
		$db->sql_freeresult($result);

		return $channel;
	}

	// Create channel
	function create_channel($channel)
	{
		global $db;

		$sql_ary = array(
			'channel_name'				=> $channel['channel_name'],
			'channel_password'			=> $channel['channel_password'],
			'last_message_id'			=> 0,
			'last_message_text'			=> '',
			'last_poster_id'			=> 0,
			'last_poster_username'		=> '',
			'last_poster_color'			=> '',
		);

		$sql = 'INSERT INTO ' . CHAT_CHANNELS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
		$db->sql_query($sql);
	}

	// Edit channel
	function edit_channel($channel_id)
	{
		global $db;

		$sql_ary = array(
		);

		$sql = 'UPDATE ' . CHAT_CHANNELS_TABLE . '
			SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
			WHERE channel_id = ' . $channel_id;
		$db->sql_query($sql);
	}

	// Delete channel
	function del_channel($channel_id)
	{
		global $db;

		$sql = 'DELETE FROM ' . CHAT_CHANNELS_TABLE . ' WHERE channel_id = ' . $channel_id;
		$db->sql_query($sql);
	}
}

?>